/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.life.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.assessment.entity.AssessmentTime;
import com.fosung.frame.modules.assessment.service.AssessmentTimeService;
import com.fosung.frame.modules.checkinfos.entity.Checkinfos;
import com.fosung.frame.modules.checkinfos.service.CheckinfosService;
import com.fosung.frame.modules.life.entity.OrganizeLife;
import com.fosung.frame.modules.life.service.OrganizeLifeService;
import com.fosung.frame.modules.partysinfo.entity.PartysInfo;
import com.fosung.frame.modules.partysinfo.service.PartysInfoService;
import com.fosung.frame.modules.personassessmentinfo.entity.PersonAssessmentInfo;
import com.fosung.frame.modules.personassessmentinfo.service.PersonAssessmentInfoService;
import com.fosung.frame.modules.score.dao.OrganizePersonDetailDao;
import com.fosung.frame.modules.score.entity.OrganizePersonDetail;
import com.fosung.frame.modules.score.entity.PersonScoreDetail;
import com.fosung.frame.modules.score.service.PersonScoreDetailService;
import com.fosung.frame.modules.sys.service.OfficeService;
import com.fosung.frame.modules.sys.utils.UserUtils;

/**
 * 组织生活管理Controller
 * 
 * @author zhangfn
 * @version 2017-08-29
 */
@Controller
@RequestMapping(value = "${adminPath}/life/personscore")
public class personQdController extends BaseController {

	@Autowired
	private OrganizeLifeService organizeLifeService;
	@Autowired
	private AssessmentTimeService assessmentTimeService;
	@Autowired
	private PersonAssessmentInfoService personAssessmentInfoService;
	@Autowired
	private PersonScoreDetailService personScoreDetailService;
	@Autowired
	private OrganizePersonDetailDao organizePersonDetailDao;
	@Autowired
	private CheckinfosService checkinfosService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private PartysInfoService partysInfoService;

	@ModelAttribute
	public OrganizeLife get(@RequestParam(required = false) String id) {
		OrganizeLife entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = organizeLifeService.get(id);
		}
		if (entity == null) {
			entity = new OrganizeLife();
		}
		return entity;
	}

	@RequiresPermissions("life:personQd:view")
	@RequestMapping(value = { "list", "" })
	public String list(OrganizeLife organizeLife, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		organizeLife.setColumn25("2");
		organizeLife.setShstate("");
		organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());
		Page<OrganizeLife> page = organizeLifeService.findPage(new Page<OrganizeLife>(request, response), organizeLife);

		model.addAttribute("page", page);
		return "modules/applyScore/personCxList";
	}

	// 跳转到签到页面
	@RequiresPermissions("life:personQd:view")
	@RequestMapping(value = "form")
	public String personQd(OrganizeLife organizeLife, Model model) {

		PartysInfo p = partysInfoService.getByOrgid(UserUtils.getUser().getOffice().getId());
		if (p == null) {
			return "modules/error";
		} else {

			// 获取年份list
			AssessmentTime assessmentTime = new AssessmentTime();
			assessmentTime.setState("1");
			assessmentTime.setDelFlag("0");
			List<AssessmentTime> assessmentTimeList = assessmentTimeService.findList(assessmentTime);
			organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());
			organizeLife.setOrganizationName(UserUtils.getUser().getOffice().getName());

			int personCount = organizePersonDetailDao.getPersonCount(organizeLife.getId());
			if (personCount == 0) {
				Map map = organizeLifeService.getQd("2");
				if (map != null) {
					model.addAttribute("partyAssessmentInfoList", map.get("partyAssessmentInfoList"));
					model.addAttribute("personAssessmentInfoList", map.get("personAssessmentInfoList"));
				}
				organizeLife.setOrganizationName(officeService.get(UserUtils.getUser().getOffice().getId()).getName());
				model.addAttribute("organizeLife", organizeLife);
				model.addAttribute("assessmentTimeList", assessmentTimeList);

				return "modules/applyScore/personQd";
			} else {
				Map map = organizeLifeService.upQd(organizeLife.getId());

				organizeLife = organizeLifeService.get(organizeLife.getId());
				if (map != null) {
					model.addAttribute("dyfz", map.get("dyfz"));
					model.addAttribute("partyScoreDetailList", map.get("partyScoreDetailList"));
					model.addAttribute("personScoreDetailList", map.get("personScoreDetailList"));
					model.addAttribute("organizePersonDetailList", map.get("organizePersonDetailList"));
				}
				Map mapSelect = organizeLifeService.getQd("2");// 获取所有的下拉框的值
				if (mapSelect != null) {
					model.addAttribute("partyAssessmentInfoList", mapSelect.get("partyAssessmentInfoList"));
					model.addAttribute("personAssessmentInfoList", mapSelect.get("personAssessmentInfoList"));
				}
				organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());
				organizeLife.setOrganizationName(UserUtils.getUser().getOffice().getName());
				model.addAttribute("organizeLife", organizeLife);
				model.addAttribute("assessmentTimeList", assessmentTimeList);
				return "modules/applyScore/upPersonQd";
			}
		}

	}

	@RequiresPermissions("life:organizaQd:view")
	@RequestMapping(value = "queryInfo")
	public String queryInfo(OrganizeLife organizeLife, Model model) throws ParseException {
		PartysInfo p = partysInfoService.getByOrgid(UserUtils.getUser().getOffice().getId());
		if (p == null) {
			return "modules/error";
		} else {
			Map map = organizeLifeService.upQd(organizeLife.getId());

			organizeLife = organizeLifeService.get(organizeLife.getId());
			if (map != null) {
				model.addAttribute("dyfz", map.get("dyfz"));
				model.addAttribute("partyScoreDetailList", map.get("partyScoreDetailList"));
				model.addAttribute("personScoreDetailList", map.get("personScoreDetailList"));
				model.addAttribute("organizePersonDetailList", map.get("organizePersonDetailList"));
			}
			Map mapSelect = organizeLifeService.getQd("2");// 获取所有的下拉框的值
			if (mapSelect != null) {
				model.addAttribute("partyAssessmentInfoList", mapSelect.get("partyAssessmentInfoList"));
				model.addAttribute("personAssessmentInfoList", mapSelect.get("personAssessmentInfoList"));
			}
			organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());
			organizeLife.setOrganizationName(UserUtils.getUser().getOffice().getName());

			Map<Integer, String> yyzlmap = new HashMap<Integer, String>();
			if (organizeLife.getYyzl() != null && !organizeLife.getYyzl().equals("")) {
				String[] yyzl = organizeLife.getYyzl().split("\\|");
				for (int i = 1; i < yyzl.length; i++) {
					yyzlmap.put(i, yyzl[i]);
				}
				model.addAttribute("yyzlmap", yyzlmap);
			}
			// 循环获取审批意见，然后赋值
			Checkinfos ci = new Checkinfos();
			ci.setOrganizeLifeId(organizeLife.getId());
			List<Checkinfos> listCheckinfos = checkinfosService.findList(ci);
			if (listCheckinfos.size() > 0) {
				String str = "";
				for (Checkinfos s : listCheckinfos) {
					Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK)
							.parse(s.getCreateDate().toString());
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					String sDate = sdf.format(date);

					str += "审核时间：" + sDate + ",审核人：" + s.getShName() + ",审核意见:" + s.getApproveContent() + "\n";
				}
				organizeLife.setShyj(str);
			}
			model.addAttribute("organizeLife", organizeLife);
			return "modules/applyScore/personQdForm";
		}

	}

	// 根据id获取党员积分内容
	@ResponseBody
	@RequestMapping(value = "/queryPersonAssessmentInfo/{id}", method = RequestMethod.GET)
	public PersonAssessmentInfo queryPersonAssessmentInfo(@PathVariable("id") String id) {
		PersonAssessmentInfo p = new PersonAssessmentInfo();
		p.setId(id);
		p = personAssessmentInfoService.get(p);
		PersonScoreDetail personScoreDetail = new PersonScoreDetail();
		personScoreDetail.setOrganizeId(UserUtils.getUser().getOffice().getId());
		personScoreDetail.setPersonAssessmentInfoId(id);
		PersonScoreDetail appCount = personScoreDetailService.getSqcs(personScoreDetail);
		if (appCount.getApplyCount() == null || appCount.getApplyCount().equals("")) {
			p.setApplyCount("0");
		} else {
			p.setApplyCount(String.valueOf(appCount.getApplyCount()));
		}
		return p;
	}

	@RequestMapping(value = "qd")
	@ResponseBody
	public Map<String, String> qd(@RequestBody OrganizeLife organizeLife, Model model,
			RedirectAttributes redirectAttributes) throws ParseException {
		for (int i = 0; i < organizeLife.getPersonScoreDetailList().size(); i++) {
			organizeLife.getPersonScoreDetailList().get(i).setOrganizeId(UserUtils.getUser().getOffice().getId());
		}
		List<OrganizePersonDetail> lists = organizeLife.getOrganizePersonDetailList1();

		for (int i = 0; i < lists.size(); i++) {
			OrganizePersonDetail o = lists.get(i);
			o.setOrganizeId(UserUtils.getUser().getOffice().getId());
		}
		Map<String, String> map = new HashMap<String, String>();
		organizeLifeService.personqd(organizeLife);
		map.put("type", "success");
		map.put("msg", "添加成功！");
		return map;
	}

	@RequestMapping(value = "upqd")
	@ResponseBody
	public Map<String, String> upqd(@RequestBody OrganizeLife organizeLife, Model model,
			RedirectAttributes redirectAttributes) throws ParseException {
		for (int i = 0; i < organizeLife.getPersonScoreDetailList().size(); i++) {
			organizeLife.getPersonScoreDetailList().get(i).setOrganizeId(UserUtils.getUser().getOffice().getId());
		}
		List<OrganizePersonDetail> lists = organizeLife.getOrganizePersonDetailList1();

		for (int i = 0; i < lists.size(); i++) {
			OrganizePersonDetail o = lists.get(i);
			o.setOrganizeId(UserUtils.getUser().getOffice().getId());
		}
		Map<String, String> map = new HashMap<String, String>();
		organizeLifeService.uppersonqd(organizeLife);
		map.put("type", "success");
		map.put("msg", "添加成功！");
		return map;
	}

	// 组织生活提交审核
	@RequiresPermissions("life:personQd:view")
	@ResponseBody
	@RequestMapping(value = "/organizeLifeTj/{ids}", method = RequestMethod.GET)
	public Map<String, String> organizeLifeTj(@PathVariable("ids") String ids, RedirectAttributes redirectAttributes) {
		Map<String, String> map = new HashMap<String, String>();

		if (ids != null && !ids.equals("")) {
			String[] organizeLifeIds = ids.split(",");

			if (organizeLifeIds != null && organizeLifeIds.length > 0) {
				OrganizeLife organizeLife = new OrganizeLife();
				for (String s : organizeLifeIds) {
					organizeLife.setId(s);
					organizeLife.setShstate("2");
					organizeLifeService.upState(organizeLife);
					map.put("type", "success");
					map.put("msg", "组织积分申请提交成功！");
				}
			} else {
				map.put("type", "error");
				map.put("msg", "组织积分申请提交失败！");
			}
		} else {
			map.put("type", "error");
			map.put("msg", "组织积分申请提交失败！");
		}

		return map;
	}
}